import {defineConfig,loadEnv} from "vite";
import vue from "@vitejs/plugin-vue";
import styleImport, {VantResolve} from "vite-plugin-style-import";
const path = require("path");
export default defineConfig(({ mode, command }) => {
    const env = loadEnv(mode, process.cwd());
    const {VITE_APP_ENV} = env;
    const hosts = {development:'localhost',production:'10.32.43.206',staging:'10.32.43.206'};

    // const targets = {development:'http://127.0.0.1:8101/oliao',production:'http://221.178.251.53:8080/oliao/token',staging:'http://10.32.43.206:34001/oliao'};
    const targets = {development:'http://127.0.0.1:8101/oliao',production:'http://221.178.251.53:8080/oliao/token/',staging:'http://10.32.43.206:34002/oliao'};

    // const baseUrls = {development:'/',production:'http://221.178.251.53:8080/satisfaction/dfgx/pdf/dist',staging:'http://221.178.251.53:8080/satisfaction/dfgx/olweb/dist'};
    const baseUrls = {development:'/',production:'http://221.178.251.53:8080/satisfaction/oliao/dist',staging:'/'};
    const ports = {development:'8100',production:'34003',staging:'34004'};
    let host = hosts[VITE_APP_ENV];
    let target = targets[VITE_APP_ENV];
    let base = baseUrls[VITE_APP_ENV];
    let port = ports[VITE_APP_ENV];
    let flag = false;
    if(VITE_APP_ENV == 'production'){
        flag = true;
    }
    console.log("host:",host);
    console.log("target",target)
    console.log("=====================",VITE_APP_ENV)
    console.log("port",port)
    return {
        presets: [["@babel/preset-env", {modules: false}]],
        plugins: [
            vue(),
            styleImport({
                resolves: [VantResolve()],
            }),

        ],
        resolve: {
            alias: {
                // 键必须以斜线开始和结束
                "@": path.resolve(__dirname, "./src"),
                "@v": path.resolve(__dirname, "./src/view"),
                "@assets": path.resolve(__dirname, "./src/assets"),
                "@api": path.resolve(__dirname, "./src/api"),
            },
        },
        /**
         * 在生产中服务时的基本公共路径。
         * @default '/'
         */
        base: base,
        server: {
            /**
             * 与“根”相关的目录，构建输出将放在其中。如果目录存在，它将在构建之前被删除。
             * @default 'dist'
             */
            host: host,
            port: port, //
            // 是否自动在浏览器打开
            open: true,
            // 是否开启 https
            https: false,
            // 服务端渲染
            ssr: false,
            // 引入第三方的配置
            proxy: {
                // 如果是 /api 打头，则访问地址如下
                "/oliao": {
                    // target: "http://127.0.0.1:8101/oliao", //后端接口地址
                    target:target,//后端接口地址
                    changeOrigin: true, //是否允许跨越
                    rewrite: (path) => path.replace(/^\/oliao/, ""),
                },
            },
        },
        build: {
            minify: "terser",
            outDir: "dist",
            assetsDir: "assets",
            cssCodeSplit: true,
            sourcemap: false,
            emptyOutDir:true,
            terserOptions: {
                compress: {
                    // 生产环境时移除console
                    drop_console: flag,
                    // drop_debugger: true
                },
            },
            chunkSizeWarningLimit: 2500,
            rollupOptions: {
                output: {
                    manualChunks(id) {
                        if (id.includes('node_modules')) {
                            // 分解块，将大块分解成更小的块,在vite.config.js当中的build下面进行配置
                            return id.toString().split('node_modules/')[1].split('/')[0].toString();
                            // 但是生成的文件都在dist下面的assets文件下，里面既有js、css等等。
                        }
                    },
                    // 可以将不同的文件放在不同的文件下
                    chunkFileNames: (chunkInfo) => {
                        const facadeModuleId = chunkInfo.facadeModuleId ? chunkInfo.facadeModuleId.split('/') : [];
                        const fileName = facadeModuleId[facadeModuleId.length - 2] || '[name]';
                        return `js/${fileName}/[name].[hash].js`;
                    }

                },
            },
        },
        css: {
            preprocessorOptions: {
                scss: {
                    // javascriptEnabled: true,
                    additionalData: '@import "./src/assets/scss/index.scss";', // 添加公共样式
                },
            },
            loaderOptions: {
                css: {},
                postcss: {
                    plugins: [
                        require("postcss-px2rem")({
                            // 以设计稿750为例， 750 / 10 = 75
                            remUnit: 72,
                        }),
                    ],
                },
            },
        }
    }
});

